package com.dkm.ec.product.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.dkm.biz.gl.domain.EavValueInt;
import com.dkm.biz.gl.persistence.dao.EavAttributeQueryDao;
import com.dkm.biz.gl.persistence.dao.EavSetQueryDao;
import com.dkm.biz.gl.persistence.dao.EavValueIntQueryDao;
import com.dkm.biz.gl.persistence.entity.EavAttributePo;
import com.dkm.biz.gl.persistence.entity.EavSetPo;
import com.dkm.biz.gl.persistence.entity.EavValueIntPo;
import com.dkm.biz.gl.repository.EavValueIntRepository;
import com.dkm.ec.home.persistence.dao.HomeEntityQueryDao;
import com.dkm.ec.home.persistence.entity.HomeEntityPo;
import com.dkm.ec.product.persistence.dao.ProductEntityQueryDao;
import com.dkm.ec.product.persistence.entity.ProductEntityPo;
import com.dkm.ec.product.service.ProdEntityService;

@Service
public class ProdEntityServiceImpl implements ProdEntityService {
	
	@Resource
	private HomeEntityQueryDao homeEntityQueryDao;
	@Resource
	private EavAttributeQueryDao eavAttributeQueryDao;
	@Resource
	private ProductEntityQueryDao productEntityQueryDao;
	@Resource
	private EavValueIntQueryDao eavValueIntQueryDao;
	@Resource
	private EavSetQueryDao eavSetQueryDao;
	@Resource
	EavValueIntRepository eavValueIntRepository;
	private transient static Log	logger			= LogFactory.getLog(ProdEntityServiceImpl.class);
	
public void autoInc(String attrKey,String prodType,String homeType,String prodId, Integer interval){
	if(attrKey==null) return;
	if(interval==null) interval=1;
	//get attrid
	Map<String,Object> params = new HashMap<String, Object>();
	 params.put("whereSql", "key_='"+attrKey+"'");
	 List<EavAttributePo> eavAttributePos = eavAttributeQueryDao.findAll(params);
	 if(eavAttributePos!=null&&eavAttributePos.size()>0){
		 EavAttributePo eavAttributePo = eavAttributePos.get(0);
		 if(eavAttributePo==null) {
			 logger.error("[error] autoInc method excuted: no attribute with "+attrKey);
			 return; 
		 }
		 Map<String,Object> param = new HashMap<String, Object>();
		 StringBuffer sb = new StringBuffer(" 1=1 ");
		 if(!StringUtils.isEmpty(prodType)){
			 sb.append(" AND type_='");
			 sb.append(prodType);
			 sb.append("'");
		 }
		 if(!StringUtils.isEmpty(prodId)){
			 sb.append(" AND id_='");
			 sb.append(prodId);
			 sb.append("'");
		 }
		 if(!StringUtils.isEmpty(homeType)){
				sb.append(" AND home_type_='");
				sb.append(homeType);
				sb.append("'");
				param.put("whereSql", sb.toString());
				List<HomeEntityPo> homeEntityPos = homeEntityQueryDao.findAll(param);
				for(HomeEntityPo homeEntityPo:homeEntityPos){
					ProductEntityPo productEntityPo = new ProductEntityPo();
					productEntityPo.setId(homeEntityPo.getHomeId());
					productEntityPo.setEavSetKey(homeEntityPo.getEavSetKey());
					
					updateAttrValue(productEntityPo, eavAttributePo, interval);
				}
		 }else{
			 param.put("whereSql", sb.toString());
			 List<ProductEntityPo> productEntityPos = productEntityQueryDao.findAll(param);
			 for(ProductEntityPo productEntityPo:productEntityPos){
				 updateAttrValue(productEntityPo, eavAttributePo, interval);
			 }
		 }
	 }
	
}
public void updateAttrValue(ProductEntityPo productEntityPo,EavAttributePo eavAttributePo,Integer intValue){
	 Map<String,Object> params=new HashMap<String,Object>();
		params.put("entityId",productEntityPo.getId());
		params.put("attrId", eavAttributePo.getId());
		EavValueIntPo oldPo = eavValueIntQueryDao.getByKey("findByProdAttr", params);
		
		if(oldPo!=null){
			if(oldPo.getValue()!=null){
				oldPo.setValue(oldPo.getValue()+Long.valueOf(String.valueOf(intValue)).longValue());
			}else{
				oldPo.setValue(Long.valueOf(String.valueOf(intValue)).longValue());
			}
			EavValueInt eavValueInt = eavValueIntRepository.newInstance(oldPo);
			eavValueInt.save();
		}else{
			EavValueIntPo eavValueIntPo = new EavValueIntPo();
			eavValueIntPo.setAttrId(eavAttributePo.getId());
			eavValueIntPo.setAttrKey(eavAttributePo.getKey());
			eavValueIntPo.setRelType(eavAttributePo.getRelType());
			eavValueIntPo.setEntityId(productEntityPo.getId());
			eavValueIntPo.setSetKey(productEntityPo.getEavSetKey());
			//get set id
			params=new HashMap<String,Object>();
			params.put("setKey", productEntityPo.getEavSetKey());
			EavSetPo	eavSetPo = eavSetQueryDao.getByKey("findBySetKey", params);
			if(eavSetPo!=null)eavValueIntPo.setSetId(eavSetPo.getId());
			eavValueIntPo.setValue(Long.valueOf(String.valueOf(intValue)).longValue());
			EavValueInt eavValueInt = eavValueIntRepository.newInstance(eavValueIntPo);
			eavValueInt.save();
		}
	}
}
